package com.sdr.dahuavideo.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.sdr.dahuavideo.DahuaMainActivity;
import com.sdr.xitang.R;
import com.sdr.dahuavideo.bean.NodeBean;
import com.sdr.dahuavideo.tree.bean.Node;
import com.sdr.dahuavideo.tree.bean.TreeListViewAdapter;

import java.util.List;

/**
 * Created by Administrator on 2016/9/27.
 */
public class DeviceTreeAdapter<T> extends TreeListViewAdapter<T> implements TreeListViewAdapter.OnTreeNodeClickListener{

    private List<NodeBean> nodeBeanList;
    private Context context;

    public DeviceTreeAdapter(ListView mTree, Context context, List<T> datas, int defaultExpandLevel) throws IllegalArgumentException, IllegalAccessException {
        super(mTree, context, datas, defaultExpandLevel);
        nodeBeanList = (List<NodeBean>) datas;
        this.context = context;
    }

    @Override
    public View getConvertView(final Node node, final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (convertView == null)
        {
            convertView = mInflater.inflate(R.layout.device_list_item, viewGroup, false);
            viewHolder = new ViewHolder();
            viewHolder.icon = (ImageView) convertView
                    .findViewById(R.id.id_treenode_icon);
            viewHolder.iv_treenode_type = (ImageView) convertView.findViewById(R.id.iv_treenode_type);
            viewHolder.label = (TextView) convertView
                    .findViewById(R.id.id_treenode_label);
            convertView.setTag(viewHolder);
        } else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if ("通道".equals(node.getNodeType())){
            viewHolder.iv_treenode_type.setImageResource(R.drawable.dahua_devicelist_channel);
        }else if ("组织".equals(node.getNodeType())){
            viewHolder.iv_treenode_type.setImageResource(R.drawable.dahua_devicelist_group);
        }else if ("设备".equals(node.getNodeType())){
            viewHolder.iv_treenode_type.setImageResource(R.drawable.dahua_devicelist_device);
        }

        if (node.getIcon() == -1)
        {
            viewHolder.icon.setVisibility(View.INVISIBLE);
        } else
        {
            viewHolder.icon.setVisibility(View.VISIBLE);
            viewHolder.icon.setImageResource(node.getIcon());
        }
        viewHolder.label.setTextColor(Color.parseColor("#000000"));
        viewHolder.label.setText(node.getName());

        return convertView;
    }

    @Override
    public void onClick(Node node, int position) {
        if ("通道".equals(node.getNodeType())) {
            Log.e("通道内容",node.getChannelId()+">>>"+node.getChannelName());
            if (DahuaMainActivity.mainActivity.mDrawerLayout.isDrawerOpen(DahuaMainActivity.mainActivity.mScrollView)) {
                DahuaMainActivity.mainActivity.mDrawerLayout.closeDrawers();
            }
            if (DahuaMainActivity.mainActivity.clickGridViewPosition>=0) {
                DahuaMainActivity.mainActivity.addControllerMap(DahuaMainActivity.mainActivity.clickGridViewPosition, node.getChannelId(), node.getChannelName());
            }
        }
    }

    private final class ViewHolder
    {
        ImageView icon;
        ImageView iv_treenode_type;
        TextView label;
    }
}
